import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class TreeEvent(QMainWindow):
    def __init__(self, parent=None):
        super(TreeEvent, self).__init__(parent)

        self.setWindowTitle("位数节点添加响应控件")
        self.tree = QTreeWidget()
        self.setCentralWidget(self.tree)

        self.tree.setColumnCount(2)
        self.tree.setHeaderLabels(['Key', 'value'])

        root = QTreeWidgetItem(self.tree)
        root.setText(0, 'root')
        root.setText(1, '1')

        # flies = QTreeWidgetItem(self.tree)
        # flies.setText(0, 'flies')
        # flies.setText(1, 'open')

        child1 = QTreeWidgetItem(root)
        child1.setText(0, 'child1')
        child1.setText(1, '1')

        self.tree.clicked.connect(self.onTreeClicked)
    def onTreeClicked(self):
        item = self.tree.currentItem()
        print('key = %s, and Value = %s' % (item.text(0), item.text(1)))
if __name__ == '__main__':

    app = QApplication(sys.argv)
    exec_ = TreeEvent()
    exec_.show()
    sys.exit(app.exec_())